<template>
  <!-- 基于 Element UI 新增和修改弹窗 -->
  <el-dialog :title="!dataForm.aid ? '添加' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <!-- 新增和创建表单表单 -->
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataSubmit()" label-width="80px">
      <el-form-item label="代理商级别" prop="alId">
        <el-select v-model="dataForm.alId" placeholder="请选择">
          <el-option v-for="item in agentLevelOptions" :key="item.alId" :label="item.agentName" :value="item.alId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="代理名称" prop="agentName">
        <el-input v-model="dataForm.agentName" placeholder="代理名称"></el-input>
      </el-form-item>
      <el-form-item label="不显示规格名称" prop="limitGoodName">
        <el-input v-model="dataForm.limitGoodName" placeholder="不显示包含的规格名称得价格和库存 多个可用, 分隔"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="地址"></el-input>
      </el-form-item>
      <el-form-item label="纬度" prop="lat">
        <el-input-number :min="-90" :max="90" v-model="dataForm.lat" placeholder="纬度"></el-input-number>
      </el-form-item>
      <el-form-item label="经度" prop="lng">
        <el-input-number :min="-180" :max="180" v-model="dataForm.lng" placeholder="经度"></el-input-number>
      </el-form-item>
      <el-form-item label="电话" prop="tel">
        <el-input v-model="dataForm.tel" placeholder="电话"></el-input>
      </el-form-item>
      <el-form-item label="城市" prop="cityName">
        <el-input v-model="dataForm.cityName" placeholder="城市"></el-input>
      </el-form-item>
      <el-form-item label="账期" prop="payLimitDay">
        <el-input-number :min="1" v-model="dataForm.payLimitDay" placeholder="账期"></el-input-number>
        <el-date-picker v-model="dataForm.payLimitTime" type="datetime" placeholder="选择日期">
        </el-date-picker>
        <el-switch v-model="dataForm.usePld" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
          :inactive-value="0">
        </el-switch>
      </el-form-item>
      <el-form-item label="额度" prop="payLimit">
        <el-input-number :min="1" v-model="dataForm.payLimit" placeholder="额度"></el-input-number>
        <el-switch v-model="dataForm.usePl" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
          :inactive-value="0">
        </el-switch>
      </el-form-item>
      <el-form-item label="小程序展示" prop="showMini">
        <el-switch v-model="dataForm.showMini" :active-value="1" :inactive-value="0" active-color="#13ce66"
          inactive-color="#ff4949">
        </el-switch>
      </el-form-item>
      <el-form-item label="提醒" prop="reminder">
        <el-input v-model="dataForm.reminder" placeholder="额度用完的提醒"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import * as api from '@/api/agent'
import { agentLevelAll } from '@/api/agentlevel'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        aid: 0,
        alId: '',
        agentName: '',
        limitGoodName: '',
        address: '',
        tel: '',
        cityName: '',
        payLimitTime: '',
        showMini: 1,
        createTime: '',
        updateTime: '',
        remark: ''
      },
      dataRule: {
        alId: [
          { required: true, message: '代理商级别id  为必填项', trigger: 'blur' }
        ],
        agentName: [
          { required: true, message: '代理名称  为必填项', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '地址  为必填项', trigger: 'blur' }
        ],
        tel: [
          { required: true, message: '电话  为必填项', trigger: 'blur' }
        ],
        cityName: [
          { required: true, message: '城市  为必填项', trigger: 'blur' }
        ]
      },
      agentLevelOptions: []
    }
  },
  created() {
    this.getAgentLevelAll()
  },
  methods: {
    getAgentLevelAll() {
      agentLevelAll().then(res => {
        this.agentLevelOptions = res
      })
    },
    init(id) {
      // 初始化表单验证规则
      this.dataForm.aid = id || 0
      console.log("初始化", this.dataForm.aid);
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.aid) {
          api.agentDetailApi(this.dataForm.aid).then(res => {

            this.dataForm = res;
          })
        }
      })
    },
    // 表单数据提交
    dataSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.dataForm.aid) {
            api.agentUpdateApi(this.dataForm).then(res => {
              // TODO 保存数据
              this.$emit('refreshDataList', "form")
            });
          } else {
            api.AgentCreateApi(this.dataForm).then(res => {
              // TODO 保存数据
              this.$emit('refreshDataList', "form")
            });
          }

        }
      })
    }
  }
}
</script>
